package com.ncmmall.mall.controller.business.marketing.groupbooking;

import com.ncmmall.domain.business.order.groupbooking.GroupBooking;
import com.ncmmall.domain.business.order.groupbooking.GroupBookingOrder;
import com.ncmmall.domain.business.order.groupbooking.QGroupBooking;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Nullable;
import java.util.Map;

/**
 * @author linxs
 * @Description: 拼团活动
 * @Date: 2020-02-03 14:35
 */
@Controller
@RequestMapping(value = "/merchant/groupBooking/order")
public class GroupBookingOrderController {

    private final static Logger logger = LoggerFactory.getLogger(GroupBookingOrderController.class);

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 商品列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private String list(Model model, PageParams pageParams, GroupBooking groupBooking) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", Securitys.currentUser().getId());
        String hql = builderSQL("select u from GroupBooking u where u.merchant.id = :id and u.status != 0 ", params, groupBooking);
        QueryPage<GroupBooking> page = queryChannelService.findPage(hql, pageRequest.setParams(params), GroupBooking.class);

        model.addAttribute("page", page).addAttribute("params", groupBooking);

        return "merchant/marketing/groupBooking/order/list";
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
    private String detail(Model model, @PathVariable("id") long id) {

        GroupBooking groupBooking = queryChannelService.findOne(QGroupBooking.groupBooking.id.eq(id), GroupBooking.class);
        groupBooking.setDetails(Lists.transform(groupBooking.getDetails(), new Function<GroupBookingOrder, GroupBookingOrder>() {
            @Nullable
            @Override
            public GroupBookingOrder apply(@Nullable GroupBookingOrder input) {

                if (input.getStatus() == 5) {
                    return null;
                }

                return input;
            }
        }));

        model.addAttribute("groupBooking", groupBooking);

        return "merchant/marketing/groupBooking/order/detail";

    }


    /**
     * 条件筛选
     */
    private String builderSQL(String sql, Map<String, Object> params, GroupBooking goods) {

        if (StringUtils.isNotBlank(goods.getGroupBookingNo())){
            sql += " and u.groupBookingNo = :groupBookingNo";
            params.put("groupBookingNo", goods.getGroupBookingNo());
        }

        sql += " order by u.createTime desc ";

        return sql;
    }


}
